package com.aaa.controller.back;

import com.aaa.entity.Category;
import com.aaa.entity.Goods;
import com.aaa.entity.GoodsSize;
import com.aaa.service.back.CategoryService;
import com.aaa.service.back.GoodsService;
import com.aaa.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/goods")
@Controller
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private CategoryService categoryService;

    /**
     * 商品信息显示
     * @param model
     * @return
     */
    @RequestMapping("/findGoods")
    public String findGoods(Model model){
        List<Map<String, Object>> goodsList = goodsService.findGoods();
        model.addAttribute("goodsList",goodsList);
        return "back/goods-list";
    }

    /**
     * 商品分类信息显示
     * @param model
     * @return
     */
    @RequestMapping("/findCategory")
    public String findCategory(Model model){
        List<Map<String, Object>> categoryList = goodsService.findCategory();
        model.addAttribute("categoryList",categoryList);
        return "back/category-list";
    }

    /**
     * 添加商品分类显示
     * @return
     */
    @RequestMapping("/addCategoryShow")
    public String addCategoryShow(){
        return "back/category-add";
    }

    /**
     * 添加商品分类
     * @return
     */
    @RequestMapping("/addCategorySubmit")
    public String addCategorySubmit(Category category){
        categoryService.addCategory(category);
        return "redirect:/goods/addCategorySubmit";
    }

    /**
     * 修改商品分类显示
     * @return
     */
    @RequestMapping("/editCategoryShow")
    public String editCategoryShow(Model model,Integer categoryId){
        Map<String, Object> categoryMap = categoryService.findCategoryBuCategoryId(categoryId);
        model.addAttribute("categoryMap",categoryMap);
        return "back/category-edit";
    }

    /**
     * 修改商品分类
     * @return
     */
    @RequestMapping("/editCategorySubmit")
    public String editCategorySubmit(Category category){
        categoryService.editCategory(category);
        return "redirect:/goods/editCategorySubmit";
    }

    /**
     * 规格信息显示
     * @param model
     * @return
     */
    @RequestMapping("/findGoodsSize")
    public String findGoodsSize(Model model){
        List<Map<String, Object>> goodsSizeList = goodsService.findGoodsSize();
        model.addAttribute("goodsSizeList",goodsSizeList);
        return "back/goodsSize-list";
    }

    /**
     * 添加商品页面显示
     * @param model
     * @return
     */
    @RequestMapping("/addGoodsShow")
    public String addGoodsShow(Model model){
        List<Map<String, Object>> categoryList = goodsService.findCategory();
        model.addAttribute("categoryList",categoryList);
        List<Map<String, Object>> goodsSizeList = goodsService.findGoodsSize();
        model.addAttribute("goodsSizeList",goodsSizeList);
        return "back/goods-add";
    }

    //提交添加
    @RequestMapping("/addGoodsSubmit")
    public String addGoodsSubmit(String goodsName, Double retailPrice,Double tradePrice,
                              Integer goodsSizeId, Integer categoryId,
                              @RequestParam("gphoto") MultipartFile gphoto) {

        String upload = UploadUtil.uplaod(gphoto);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = "SP"+sdf.format(new Date());

        Goods goods = new Goods();

        goods.setGoodsPhoto(upload);

        goods.setGoodsId(strDate);
        goods.setGoodsName(goodsName);
        goods.setRetailPrice(retailPrice);
        goods.setTradePrice(tradePrice);


        Category category = new Category();
        category.setCategoryId(categoryId);

        GoodsSize goodsSize = new GoodsSize();
        goodsSize.setGoodsSizeId(goodsSizeId);

        goods.setGoodsSize(goodsSize);
        goods.setCategory(category);

        int count = goodsService.addGoods(goods);
        return "back/goods-add";
    }

        /**
         * 启用商品，修改商品状态为0：启用
         * @param goods
         * @return
         */
    @RequestMapping("/editGoods0")
    public String editGoods0(Goods goods){
        goodsService.editGoods0(goods);
        return "back/goods-list";
    }

    /**
     * 停用商品，修改商品状态为1：启用
     * @param goods
     * @return
     */
    @RequestMapping("/editGoods1")
    public String editGoods1(Goods goods){
        goodsService.editGoods1(goods);
        return "back/goods-list";
    }





    /**
     * 添加规格显示
     * @return
     */
    @RequestMapping("/addGoodsSizeShow")
    public String addGoodsSizeShow(){
        return "back/goodsSize-add";
    }

    /**
     * 添加规格
     * @return
     */
    @RequestMapping("/addGoodsSizeSubmit")
    public String addGoodsSizeSubmit(GoodsSize goodsSize){
        categoryService.addGoodsSize(goodsSize);
        return "redirect:/goods/addGoodsSizeSubmit";
    }

    /**
     * 修改规格显示
     * @return
     */
    @RequestMapping("/editGoodsSizeShow")
    public String editGoodsSizeShow(Model model,Integer goodsSizeId){
        Map<String, Object> goodsSizeMap = categoryService.findGoodsSizeByGoodsSizeId(goodsSizeId);
        model.addAttribute("goodsSizeMap",goodsSizeMap);
        return "back/goodsSize-edit";
    }

    /**
     * 修改规格
     * @return
     */
    @RequestMapping("/editGoodsSizeSubmit")
    public String editGoodsSizeSubmit(GoodsSize goodsSize){
        categoryService.editGoodsSize(goodsSize);
        return "redirect:/goods/editGoodsSizeShow?goodsSizeId="+goodsSize.getGoodsSizeId();
    }

}
